using WebapiTemplate.Application.Common.Interface;
using WebapiTemplate.Domain.Base;
using Microsoft.EntityFrameworkCore;

/// <summary>
/// 操作表的实现类，继承于操作表的接口类
/// </summary>
namespace WebapiTemplate.Infrastructure.Persistence.Repository
{
    public class EfRepository<T> : IRepository<T> where T : BaseEntity
    {
        //定义数据库上下文实例字段
        private readonly WebapiDbcontext _db;

        //定义T表实例字段
        public DbSet<T> _table;

        //定义SessionUser解析Claim服务实例字段
        public readonly ISessionUserService _sessionUserService;

        //依赖注入得到字段的真正实例
        public EfRepository(WebapiDbcontext db, ISessionUserService sessionUserService)
        {
            _db = db;
            _table = _db.Set<T>();
            _sessionUserService = sessionUserService;
        }

        //实现接口查询整表的方法
        public IQueryable<T> Table => _table.AsNoTracking();

        #region  实现接口根据id查询方法
        public async Task<T?> FindByid(Guid id)
        {
            var entity = await _table.FindAsync(id);
            return entity;
        }
        #endregion




        #region 实现接口新增方法
        //单条新增
        public async Task<T> AddAsync(T entity)
        {
            entity.CreatedAt = DateTime.Now;
            entity.UpdatedAt = DateTime.Now;
            entity.CreatedBy = _sessionUserService.UserId;
            // entity.UpdatedBy;
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.DisplayOrder = 0;
            entity.Remarks = "简单介绍一下^3^";

            await _table.AddAsync(entity);
            await _db.SaveChangesAsync();

            return entity;
        }

        //批量新增
        public async Task<IEnumerable<T>> AddBulkAsync(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.CreatedAt = DateTime.Now;
                entity.UpdatedAt = DateTime.Now;
                entity.CreatedBy = _sessionUserService.UserId;
                entity.UpdatedBy = _sessionUserService.UserId;
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.DisplayOrder = 0;
                entity.Remarks = "自我介绍一下吧^0^";
            }
            await _table.AddRangeAsync(entities);
            await _db.SaveChangesAsync();

            return entities;
        }
        #endregion




        #region 实现接口更新方法
        //单条更新
        public async Task<T> UpdateAsync(T entity)
        {
            entity.UpdatedAt = DateTime.Now;
            entity.UpdatedBy = _sessionUserService.UserId;
            _table.Update(entity);
            await _db.SaveChangesAsync();

            return entity;
        }

        //批量更新
        public async Task<IEnumerable<T>> UpdateAsync(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.UpdatedAt = DateTime.Now;
                entity.UpdatedBy = _sessionUserService.UserId;
            }
            _table.UpdateRange(entities);
            await _db.SaveChangesAsync();

            return entities;
        }
        #endregion




        #region  实现接口删除方法
        //单条根据id删除(isRealDelete = false默认伪删除)---这个我就不写返回值了
        public async Task DeleteByIdAsync(Guid id, bool isRealDelete = false)
        {
            var entity = await this.FindByid(id);
            if (entity != null)
            {
                await this.DeleteByentityAsync(entity, isRealDelete);
            }
           
        }

        //单条根据实体entity删除(isRealDelete = false默认伪删除)
        public async Task<T> DeleteByentityAsync(T entity, bool isRealDelete = false)
        {
            if (isRealDelete)
            {
                _table.Remove(entity);
            }
            else
            {
                entity.IsDeleted = true;
                entity.IsActived = false;
                entity.UpdatedAt = DateTime.Now;
                entity.UpdatedBy =
                    _sessionUserService.UserId != null
                        ? _sessionUserService.UserId
                        : new Guid("669ADFDA-DE50-4568-7D18-08DA5FBDE1E2");
                _table.Update(entity);
            }
            await _db.SaveChangesAsync();

            return entity;
        }

        //批量删除(isRealDelete = false默认伪删除)
        public async Task<IEnumerable<T>> DeleteBulkAsync(
            IEnumerable<T> entities,
            bool isRealDelete = false
        )
        {
            if (isRealDelete)
            {
                _table.RemoveRange(entities);
            }
            else
            {
                foreach (var entity in entities)
                {
                    entity.IsDeleted = true;
                    entity.IsActived = false;
                    entity.UpdatedAt = DateTime.Now;
                    entity.UpdatedBy = _sessionUserService.UserId;
                }
                _table.UpdateRange(entities);
            }
            await _db.SaveChangesAsync();

            return entities;
        }
        #endregion
    }
}
